package zzs.generalframework.integration.core.http.client.general.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import zzs.generalframework.integration.common.constant.HttpMethod;
import zzs.generalframework.integration.core.http.client.general.IGeneralHttpClient;

/**
 * @title
 * @description
 * @project zzs-integration
 * @package zzs.generalframework.integration.core.http.client.general.impl
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class GeneralHttpClientImpl implements IGeneralHttpClient
{
	private Logger logger = LoggerFactory.getLogger(GeneralHttpClientImpl.class);

	// @Override
	public String request(String schema, String hostname, String port, String appPath, Map<String, Object> parameterMap, HttpMethod httpMethod)
	{
		logger.info("====请求HTTP开始====");
		logger.debug("====请求HTTP,入参:String=[{}],String=[{}],String=[{}],String=[{}],Map<String,Object> parameterMap=[{}],HttpMethod=[{}]====", schema, hostname, port, appPath, parameterMap, httpMethod);
		String returnString = null;

		String urlPath = schema + "://" + hostname + ":" + port + "/" + appPath;

		if (HttpMethod.GET == httpMethod)
		{
			returnString = requestGet(urlPath, parameterMap);
		}
		else if (HttpMethod.POST == httpMethod)
		{
			returnString = requestPost(urlPath, parameterMap);
		}

		logger.debug("====请求HTTP,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTP结束===");
		return returnString;
	}

	// @Override
	public String request(String urlPath, Map<String, Object> parameterMap, HttpMethod httpMethod)
	{
		logger.info("====请求HTTP开始====");
		logger.debug("====请求HTTP,入参:String=[{}],Map<String, Object>=[{}],HttpMethod=[{}]====", urlPath, parameterMap, httpMethod);
		String returnString = null;

		if (HttpMethod.GET == httpMethod)
		{
			returnString = requestGet(urlPath, parameterMap);
		}
		else if (HttpMethod.POST == httpMethod)
		{
			returnString = requestPost(urlPath, parameterMap);
		}

		logger.debug("====请求HTTP,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTP结束===");
		return returnString;
	}

	// @Override
	public String requestGet(String schema, String hostname, String port, String appPath, Map<String, Object> parameterMap)
	{
		logger.info("====请求HTTPGET开始====");
		logger.debug("====请求HTTPGET,入参:String=[{}],String=[{}],String=[{}],String=[{}],Map<String, Object>=[{}]====", schema, hostname, port, appPath, parameterMap);
		String returnString = null;

		String urlPath = schema + "://" + hostname + ":" + port + "/" + appPath;

		returnString = requestGet(urlPath, parameterMap);

		logger.debug("====请求HTTPGET,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTPGET结束===");
		return returnString;
	}

	// @Override
	public String requestGet(String urlPath, Map<String, Object> parameterMap)
	{
		logger.info("====请求HTTPGET开始====");
		logger.debug("====请求HTTPGET,入参:String=[{}],Map<String, Object>=[{}]====", urlPath, parameterMap);
		String returnString = null;

		HttpGet httpGet = null;
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		try
		{
			if (parameterMap != null && parameterMap.size() > 0)
			{
				String parameter = "";
				for (Entry<String, Object> itemEntry : parameterMap.entrySet())
				{
					String key = itemEntry.getKey();
					String value = String.valueOf(itemEntry.getValue());
					parameter += "&" + key + "=" + value;
				}
				if (parameter.length() > 0)
				{
					parameter = parameter.substring(1);
					urlPath += "?" + parameter;
				}
			}

			httpGet = new HttpGet(urlPath);
			httpClient = HttpClients.createDefault();
			httpResponse = httpClient.execute(httpGet);
			HttpEntity responseHttpResponse = httpResponse.getEntity();
			if (responseHttpResponse != null)
			{
				returnString = EntityUtils.toString(responseHttpResponse, "UTF-8");
			}

		}
		catch (ClientProtocolException e)
		{
			logger.error("请求HTTPGET出现异常,异常原因:[{}]", e);
		}
		catch (IOException e)
		{
			logger.error("请求HTTPGET出现异常,异常原因:[{}]", e);
		}
		finally
		{
			if (httpResponse != null)
			{
				try
				{
					httpResponse.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}
			if (httpClient != null)
			{
				try
				{
					httpClient.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}

		}

		logger.debug("====请求HTTPGET,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTPGET结束===");
		return returnString;
	}

	// @Override
	public String requestGet(String urlPath)
	{
		logger.info("====请求HTTPGET开始====");
		logger.debug("====请求HTTPGET,入参:String=[{}]====", urlPath);
		String returnString = null;

		HttpGet httpGet = null;
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		try
		{
			httpGet = new HttpGet(urlPath);
			httpClient = HttpClients.createDefault();
			httpResponse = httpClient.execute(httpGet);
			HttpEntity responseHttpResponse = httpResponse.getEntity();
			if (responseHttpResponse != null)
			{
				returnString = EntityUtils.toString(responseHttpResponse, "UTF-8");
			}

		}
		catch (ClientProtocolException e)
		{
			logger.error("请求HTTPGET出现异常,异常原因:[{}]", e);
		}
		catch (IOException e)
		{
			logger.error("请求HTTPGET出现异常,异常原因:[{}]", e);
		}
		finally
		{
			if (httpResponse != null)
			{
				try
				{
					httpResponse.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}
			if (httpClient != null)
			{
				try
				{
					httpClient.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}

		}

		logger.debug("====请求HTTPGET,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTPGET结束===");
		return returnString;
	}

	// @Override
	public String requestPost(String schema, String hostname, String port, String appPath, Map<String, Object> parameterMap)
	{
		logger.info("====请求HTTPPOST开始====");
		logger.debug("====请求HTTPPOST,入参:String=[{}],String=[{}],String=[{}],String=[{}],Map<String, Object>=[{}]====", schema, hostname, port, appPath, parameterMap);
		String returnString = null;

		String urlPath = schema + "://" + hostname + ":" + port + "/" + appPath;
		returnString = requestPost(urlPath, parameterMap);

		logger.debug("====请求HTTPPOST,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTPPOST结束===");
		return returnString;
	}

	// @Override
	public String requestPost(String urlPath, Map<String, Object> parameterMap)
	{
		logger.info("====请求HTTPPOST开始====");
		logger.debug("====请求HTTPPOST,入参:String=[{}],Map<String,Object>=[{}]====", urlPath, parameterMap);
		String returnString = null;

		HttpPost httpPost = null;
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		try
		{
			httpPost = new HttpPost(urlPath);
			if (parameterMap != null && parameterMap.size() > 0)
			{
				List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
				for (Entry<String, Object> itemEntry : parameterMap.entrySet())
				{
					String key = itemEntry.getKey();
					String value = String.valueOf(itemEntry.getValue());
					parameterList.add(new BasicNameValuePair(key, value));
				}
				HttpEntity requestHttpEntity = new UrlEncodedFormEntity(parameterList);
				httpPost.setEntity(requestHttpEntity);
			}
			httpClient = HttpClients.createDefault();
			httpResponse = httpClient.execute(httpPost);
			HttpEntity responseHttpResponse = httpResponse.getEntity();
			if (responseHttpResponse != null)
			{
				returnString = EntityUtils.toString(responseHttpResponse, "UTF-8");
			}

		}
		catch (ClientProtocolException e)
		{
			logger.error("请求HTTPPOST出现异常,异常原因:[{}]", e);
		}
		catch (IOException e)
		{
			logger.error("请求HTTPPOST出现异常,异常原因:[{}]", e);
		}
		finally
		{
			if (httpResponse != null)
			{
				try
				{
					httpResponse.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}
			if (httpClient != null)
			{
				try
				{
					httpClient.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}

		}

		logger.debug("====请求HTTPPOST,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTPPOST结束===");
		return returnString;
	}

	// @Override
	public String requestPost(String urlPath)
	{
		logger.info("====请求HTTPPOST开始====");
		logger.debug("====请求HTTPPOST,入参:String=[{}]====", urlPath);
		String returnString = null;
		HttpPost httpPost = null;
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		try
		{
			httpPost = new HttpPost(urlPath);
			httpClient = HttpClients.createDefault();
			httpResponse = httpClient.execute(httpPost);
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpEntity != null)
			{
				returnString = EntityUtils.toString(httpEntity, "UTF-8");
			}

		}
		catch (ClientProtocolException e)
		{
			logger.error("请求HTTPPOST出现异常,异常原因:[{}]", e);
		}
		catch (IOException e)
		{
			logger.error("请求HTTPPOST出现异常,异常原因:[{}]", e);
		}
		finally
		{
			if (httpResponse != null)
			{
				try
				{
					httpResponse.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}
			if (httpClient != null)
			{
				try
				{
					httpClient.close();
				}
				catch (IOException e)
				{
					logger.warn("关闭流时出现异常,异常原因:[{}]", e);
				}
			}

		}

		logger.debug("====请求HTTPPOST,出参:String=[{}]====", returnString);
		logger.info("=====请求HTTPPOST结束===");
		return returnString;
	}

}
